package com.example.su_shi_tan_bas_lib.adapter.ab_list_adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter {

    private static final String TAG = "ConmerAdapter";
    private final int mItemLayoutId;
    private final Context mContext;
    private List<T> mDatas=new ArrayList<T>();

    public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {

        this.mContext = context;

        this.mDatas = mDatas;

        this.mItemLayoutId = itemLayoutId;

    }

    public CommonAdapter(Context context, int itemLayoutId) {

        this.mContext = context;



        this.mItemLayoutId = itemLayoutId;

    }


    @Override
    public int getCount() {

        return mDatas.size();
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    @Override
    public T getItem(int position) {
        if (position < 0 || position >= getCount()) return null;
        return mDatas.get(position);

    }


    @Override
    public long getItemId(int position) {

        return position;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        CommonViewHolder viewHolder = getViewHolder(position, convertView, parent);

        convert(viewHolder, getItem(position), position);

        return viewHolder.getConvertView();
    }

    public abstract void convert(CommonViewHolder helper, T item, int position);

    private CommonViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {

        return CommonViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);

    }

    public void setDataChanged(List<T> list, boolean isLoad) {

        if (isLoad) {
            this.mDatas.addAll(list);
        } else {
            this.mDatas.clear();
            this.mDatas.addAll(list);
        }
        this.notifyDataSetChanged();
    }

    public void setDataChanged(boolean isLoad, List<T> list) {
        Log.i(TAG, "list.size()=" + list.size() + "");
        if (!isLoad) {
            this.mDatas.clear();
        }
        this.mDatas.addAll(list);
        this.notifyDataSetInvalidated();
    }

    public void setDataChanged(List<T> list) {
        this.mDatas.clear();
        this.mDatas.addAll(list);
        this.notifyDataSetChanged();
    }

    public void setItemDelete(T t) {
        mDatas.remove(t);
        this.notifyDataSetChanged();
    }


    public void updateList(List<T> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    public void replace(int selectPos, int targetPos) {
        T t = mDatas.get(selectPos);
        T t1 = mDatas.get(targetPos);
        mDatas.set(selectPos, t1);
        mDatas.set(targetPos, t);
        notifyDataSetChanged();
    }


    public void addOne(T t) {
        if (t == null) return;
        this.mDatas.add(t);
        notifyDataSetChanged();
    }

    public void clears() {
        if (mDatas != null) {
            this.mDatas.clear();
            notifyDataSetChanged();
            this.mDatas = null;
        }
    }
}